import rpg_component from "../../../component/rpg_component";
import { createBuff } from "../config/testBuff";
import EffectManage from "./EffectManage";
import type { Buff, ActiveBuff } from "../types";
import { BuffConfig } from "../types";

class BuffManager {
    private buffs: Map<string, Buff> = new Map();
    private activeBuffs: Map<string, ActiveBuff[]> = new Map();

    init() {
        // 不再预加载buff，改为动态创建
    }
    addBuff(buff: Buff) {
        this.buffs.set(buff.id, buff);
    }
    
    // 动态创建并缓存buff
    createAndCacheBuff(config: BuffConfig): Buff {
        const existingBuff = this.buffs.get(config.id);
        if (existingBuff) {
            return existingBuff;
        }
        
        const buff = createBuff(config);
        this.buffs.set(buff.id, buff);
        return buff;
                        }
    
    getBuffById(id: string): Buff | undefined {
        let buff = this.buffs.get(id);
        if (!buff) {
            console.log('没有找到buff', id);
            return undefined;
        }
        return buff;
    }

    applyBuff(target: rpg_component, buffId: string, useTag: rpg_component, data: any) {
        const buff = this.getBuffById(buffId);
        if (!buff) return;

        let targetBuffs = this.activeBuffs.get(target.id);

        if (!targetBuffs) {
            targetBuffs = [];
            this.activeBuffs.set(target.id, targetBuffs);
        } else {
            targetBuffs = targetBuffs.filter(b => (b.remainingDuration > 0 || Number.isNaN(b.remainingDuration)));
        }

        const activeBuff = targetBuffs.find(b => b.buff.id === buffId);
        if (activeBuff) {
            if (Number.isNaN(buff.duration)) {
                activeBuff.remainingDuration = buff.duration;
                // 永久buff不叠加层数
            } else {
            activeBuff.remainingDuration = buff.duration;
            activeBuff.stackCount++;
            }
            // 日志：buff叠加或刷新
            console.log(`[Buff] 目标:${target.id} buff:${buff.id} 层数:${activeBuff.stackCount} 持续:${activeBuff.remainingDuration}`);
        } else {
            const newActiveBuff = { buff, remainingDuration: buff.duration, stackCount: 1, target, useTag: useTag, data: data };
            targetBuffs.push(newActiveBuff);
            target._activeBuffs.push(newActiveBuff);
            // 日志：新buff
            console.log(`[Buff] 目标:${target.id} buff:${buff.id} 层数:1 持续:${buff.duration}`);
        }
    }
    
    // 应用buff配置（从技能配置中）
    applyBuffConfig(target: rpg_component, buffConfig: BuffConfig, useTag: rpg_component, data: any) {
        const buff = this.createAndCacheBuff(buffConfig);
        this.applyBuff(target, buff.id, useTag, data);
    }
    
    clearBuff(target: rpg_component) {
        this.activeBuffs.delete(target.id);
    }
    processBuffs(target: rpg_component) {
        const targetBuffs = this.activeBuffs.get(target.id);
        if (!targetBuffs) return;
        if (target.hp_now <= 0) {
            return;
        }
        targetBuffs.forEach(activeBuff => {
            activeBuff.buff.effect(activeBuff.useTag, activeBuff.target, activeBuff.buff, activeBuff.stackCount, 0, activeBuff.data);
            activeBuff.remainingDuration--;
            if (activeBuff.remainingDuration <= 0) {
                if (activeBuff.buff.endEffect) {
                    activeBuff.buff.endEffect(activeBuff.useTag, activeBuff.target, activeBuff.buff, activeBuff.stackCount, 0, activeBuff.data);
                }
            }
        });
    }
}

export default new BuffManager();
